package com.sayice.gomoku.service.impl;

import com.sayice.gomoku.dao.AdminDao;
import com.sayice.gomoku.dao.BoardDao;
import com.sayice.gomoku.dao.BuyDao;
import com.sayice.gomoku.dao.impl.AdminDaoImpl;
import com.sayice.gomoku.dao.impl.BoardDaoImpl;
import com.sayice.gomoku.dao.impl.BuyDaoImpl;
import com.sayice.gomoku.pojo.Board;
import com.sayice.gomoku.pojo.BoardInfo;
import com.sayice.gomoku.service.BoardService;

import java.math.BigDecimal;
import java.util.List;

/**
 * 棋盘业务
 */
public class BoardServiceImpl implements BoardService {
    private final BoardDao boardDao = new BoardDaoImpl();
    private final AdminDao adminDao = new AdminDaoImpl();
    private final BuyDao buyDao = new BuyDaoImpl();
    
    @Override
    public int setName(int adminId, int boardId, String name) {
        // 更新最后修改者编号
        boardDao.updateModifier(boardId, adminId);
        // 更新棋盘名
        return boardDao.updateName(boardId, name);
    }
    
    @Override
    public int setPrice(int adminId, int boardId, BigDecimal price) {
        // 更新最后修改者编号
        boardDao.updateModifier(boardId, adminId);
        // 更新价格
        return boardDao.updatePrice(boardId, price);
    }
    
    @Override
    public int setModifier(int boardId, int modifierId) {
        return boardDao.updateModifier(boardId, modifierId);
    }
    
    @Override
    public Board getBoard(int id) {
        return boardDao.queryById(id);
    }
    
    @Override
    public List<Board> getBoards() {
        return boardDao.queryAll();
    }
    
    @Override
    public List<BoardInfo> getBoardInfos() {
        return boardDao.queryAllInfo();
    }
    
    @Override
    public List<Board> getUserBoards(int userId) {
        return boardDao.queryByUserId(userId);
    }
    
    @Override
    public boolean bought(int boardId, int userId) {
        return buyDao.exists(userId, boardId);
    }
    
    @Override
    public int buy(int userId, int boardId) {
        // 添加棋盘购买数
        boardDao.addBuyCount(boardId);
        // 添加购买记录
        return buyDao.add(userId, boardId);
    }
    
    @Override
    public boolean existsName(String name) {
        return boardDao.queryByName(name) != null;
    }
    
    @Override
    public int upload(Board board) {
        // 添加棋盘
        int count = boardDao.add(board);
        // 增加管理员上传数
        adminDao.addUploadCount(board.getUploaderId());
        return count;
    }
}
